

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class send
 */
@WebServlet("/send")
public class send extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public send() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=ISO-8859-2");
		PrintWriter out = response.getWriter();
		Boolean reklamacja = false;
		String kategoria=null;
		String topic=null;
		String mesg = request.getParameter("wiadomosc").toString();
		String nick = request.getParameter("nick").toString();
		String mail = request.getParameter("email").toString();
		if(request.getParameter("temat")!=null){
			topic = request.getParameter("temat").toString();
		}
		if(request.getParameter("kategoria")!=null) {
			kategoria=request.getParameter("kategoria").toString();
			reklamacja=true;
		}
		String host = "ssl0.ovh.net";
		String port = "465";
		out.println("Trwa wysyłanie Twojej wiadomości. Proszę czekać ... ");
		Properties props = new Properties();
		props.put("mail.smtp.user","els24@gruszowski.pl");
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.port", port);
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.socketFactory.port", port);
		props.put("mail.smtp.socketFactory.class",
		"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		System.getSecurityManager();

		try {
		Authenticator auth = new SMTPAuthenticator();
		Session session = Session.getInstance(props, auth);

		MimeMessage msg = new MimeMessage(session);
		msg.setText(mesg);
		if(reklamacja==true){
			msg.setSubject("[REKLAMACJA] "+nick+": "+kategoria+" - "+topic);
		}else{
			msg.setSubject(nick+": "+topic);
		}		
		msg.setFrom(new InternetAddress(mail,nick));
		msg.addRecipient(Message.RecipientType.TO,
		new InternetAddress("agruszowski@yahoo.com"));
		Transport.send(msg);
		} catch (Exception mex) {
		mex.printStackTrace();
		}
		if(reklamacja==true)response.sendRedirect("index.jsp?add=reklamacje&status=ok");
		else response.sendRedirect("index.jsp?add=kontakt&status=ok");
}
	private class SMTPAuthenticator extends javax.mail.Authenticator {
		public PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication("els24@gruszowski.pl", "informatyka");
		}
		}
}

